﻿#include "AmapViewImpl.h"

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

#include "platform/android/jni/JniHelper.h"

#define CLASS_NAME "com/myamap/tool/MyAmapViewHelper"

int CreateAMap(float x, float y, float width, float height)
{
	JniMethodInfo jmi;
	if (JniHelper::getStaticMethodInfo(jmi, CLASS_NAME, "createAMap", "(FFFF)I"))
	{
		return (int)(jmi.env->CallStaticIntMethod(jmi.classID, jmi.methodID, (jfloat)x, (jfloat)y, (jfloat)width, (jfloat)height));
	}

	return 0;
}

void DestroyAMap(const int tag)
{
	JniMethodInfo jmi;
	if (JniHelper::getStaticMethodInfo(jmi, CLASS_NAME, "destroyAMap", "(I)V"))
	{
		jmi.env->CallStaticVoidMethod(jmi.classID, jmi.methodID, (jint)tag);
	}
}

void SetMapCenter(const int tag, double lat, double lng)
{
	JniMethodInfo jmi;
	if (JniHelper::getStaticMethodInfo(jmi, CLASS_NAME, "setMapCenter", "(IDD)V"))
	{
		jmi.env->CallStaticVoidMethod(jmi.classID, jmi.methodID, (jint)tag, (jdouble)lat, (jdouble)lng);
	}
}

void SetMapZoomLevel(const int tag, double level)
{
	JniMethodInfo jmi;
	if (JniHelper::getStaticMethodInfo(jmi, CLASS_NAME, "setMapZoomLevel", "(ID)V"))
	{
		jmi.env->CallStaticVoidMethod(jmi.classID, jmi.methodID, (jint)tag, (jfloat)level);
	}
}

void UpdateFrame(const int tag, int x, int y, int width, int height)
{
	JniMethodInfo jmi;
	if (JniHelper::getStaticMethodInfo(jmi, CLASS_NAME, "setAMapRect", "(IIIII)V"))
	{
		jmi.env->CallStaticVoidMethod(jmi.classID, jmi.methodID, (jint)tag, (jint)x, (jint)y, (jint)width, (jint)height);
	}
}

void SetVisible(const int tag, bool visible)
{
	JniMethodInfo jmi;
	if (JniHelper::getStaticMethodInfo(jmi, CLASS_NAME, "setVisible", "(IZ)V"))
	{
		jmi.env->CallStaticVoidMethod(jmi.classID, jmi.methodID, (jint)tag, (jboolean)visible);
	}
}

void AddMarker(const int tag, const string& markertag, AmapMarker* marker)
{
	JniMethodInfo jmi;
	if (JniHelper::getStaticMethodInfo(jmi, CLASS_NAME, "addMarker", "(ILjava/lang/String;Ljava/lang/String;ZLjava/lang/String;Ljava/lang/String;DD)V"))
	{
		jstring jmarkertag = jmi.env->NewStringUTF(markertag.c_str());
		jstring jimg = jmi.env->NewStringUTF(marker->getImg().c_str());
		jstring jtitle = jmi.env->NewStringUTF(marker->getTitle().c_str());
		jstring jcontent = jmi.env->NewStringUTF(marker->getContent().c_str());

		jmi.env->CallStaticVoidMethod(jmi.classID, jmi.methodID, (jint)tag, jmarkertag, jimg, (jboolean)(marker->getDraggable()), jtitle, jcontent, (jdouble)(marker->getLat()), (jdouble)(marker->getLng()));

		jmi.env->DeleteLocalRef(jmarkertag);
		jmi.env->DeleteLocalRef(jimg);
		jmi.env->DeleteLocalRef(jtitle);
		jmi.env->DeleteLocalRef(jcontent);
	}
}

void DeleteMarker(const int tag, const string& markertag)
{
	JniMethodInfo jmi;
	if (JniHelper::getStaticMethodInfo(jmi, CLASS_NAME, "deleteMarker", "(ILjava/lang/String;)V"))
	{
		jstring jmarkertag = jmi.env->NewStringUTF(markertag.c_str());

		jmi.env->CallStaticVoidMethod(jmi.classID, jmi.methodID, (jint)tag, jmarkertag);

		jmi.env->DeleteLocalRef(jmarkertag);
	}
}

void ClearMarkers(const int tag)
{
	JniMethodInfo jmi;
	if (JniHelper::getStaticMethodInfo(jmi, CLASS_NAME, "clearMarkers", "(I)V"))
	{
		jmi.env->CallStaticVoidMethod(jmi.classID, jmi.methodID, (jint)tag);
	}
}

void StartAmapLocation(const int tag)
{
	JniMethodInfo jmi;
	if (JniHelper::getStaticMethodInfo(jmi, CLASS_NAME, "startLocation", "(I)V"))
	{
		jmi.env->CallStaticVoidMethod(jmi.classID, jmi.methodID, (jint)tag);
	}
}

void SetAmapRouteViewType(const int tag, double slat, double slng, double elat, double elng, int type)
{
	JniMethodInfo jmi;
	if (JniHelper::getStaticMethodInfo(jmi, CLASS_NAME, "setRouteViewType", "(IDDDDI)V"))
	{
		jmi.env->CallStaticVoidMethod(jmi.classID, jmi.methodID, (jint)tag, (jdouble)slat, (jdouble)slng, (jdouble)elat, (jdouble)elng, (jint)type);
	}
}

void SearchRoute(const int tag)
{
	JniMethodInfo jmi;
	if (JniHelper::getStaticMethodInfo(jmi, CLASS_NAME, "searchRoute", "(I)V"))
	{
		jmi.env->CallStaticVoidMethod(jmi.classID, jmi.methodID, (jint)tag);
	}
}

void ShowBusRouteView(const int _viewTag, const int bustag)
{
	JniMethodInfo jmi;
	if (JniHelper::getStaticMethodInfo(jmi, CLASS_NAME, "showBusRouteView", "(II)V"))
	{
		jmi.env->CallStaticVoidMethod(jmi.classID, jmi.methodID, (jint)_viewTag, (jint)bustag);
	}
}

void SetBusRouteCity(const int _viewTag, const string& city, const string& cityd)
{
	JniMethodInfo jmi;
	if (JniHelper::getStaticMethodInfo(jmi, CLASS_NAME, "setBusRouteCity", "(ILjava/lang/String;Ljava/lang/String;)V"))
	{
		jstring jcity = jmi.env->NewStringUTF(city.c_str());
		jstring jcityd = jmi.env->NewStringUTF(cityd.c_str());

		jmi.env->CallStaticVoidMethod(jmi.classID, jmi.methodID, (jint)_viewTag, jcity, jcityd);

		jmi.env->DeleteLocalRef(jcity);
		jmi.env->DeleteLocalRef(jcityd);
	}
}

void GetBusRouteStep(const int _viewTag, const int bustag)
{
	JniMethodInfo jmi;
	if (JniHelper::getStaticMethodInfo(jmi, CLASS_NAME, "getBusRouteStep", "(II)V"))
	{
		jmi.env->CallStaticVoidMethod(jmi.classID, jmi.methodID, (jint)_viewTag, (jint)bustag);
	}
}

extern "C"
{
	JNIEXPORT void JNICALL Java_com_myamap_tool_MyAmapViewHelper_onAmapLoaded(JNIEnv *env, jclass, jint index) 
	{
		AmapViewImpl::onAmapLoaded((int)index);
	}

	JNIEXPORT void JNICALL Java_com_myamap_tool_MyAmapViewHelper_onAmapLocationEnd(JNIEnv *env, jclass, jint index, jint jcode, jdouble jlat, jdouble jlng, jstring jformatAddr, jstring jprovince, jstring jcity, jstring jdistrict, jstring jstreet, jstring jnumber, jstring jpoi, jstring jadcode)
	{
		string formatAddr = JniHelper::jstring2string(jformatAddr);
		string province = JniHelper::jstring2string(jprovince);
		string city = JniHelper::jstring2string(jcity);
		string district = JniHelper::jstring2string(jdistrict);
		string street = JniHelper::jstring2string(jstreet);
		string number = JniHelper::jstring2string(jnumber);
		string poi = JniHelper::jstring2string(jpoi);
		string adcode = JniHelper::jstring2string(jadcode);

		AmapViewImpl::onAmapLocationEnd((int)index, (int)jcode, (double)jlat, (double)jlng, formatAddr, province, city, district, street, number, poi, adcode);
	}

	JNIEXPORT void JNICALL Java_com_myamap_tool_MyAmapViewHelper_onAmapClicked(JNIEnv *env, jclass, jint index, jdouble jlat, jdouble jlng)
	{
		AmapViewImpl::onAmapClicked((int)index, (double)jlat, (double)jlng);
	}

	JNIEXPORT void JNICALL Java_com_myamap_tool_MyAmapViewHelper_onAmapMarkerDragStart(JNIEnv *env, jclass, jint index, jstring jmarkertag, jdouble jlat, jdouble jlng)
	{
		string markertag = JniHelper::jstring2string(jmarkertag);

		AmapViewImpl::onAmapMarkerDragStart((int)index, markertag,(double)jlat, (double)jlng);
	}

	JNIEXPORT void JNICALL Java_com_myamap_tool_MyAmapViewHelper_onAmapMarkerDrag(JNIEnv *env, jclass, jint index, jstring jmarkertag, jdouble jlat, jdouble jlng)
	{
		string markertag = JniHelper::jstring2string(jmarkertag);

		AmapViewImpl::onAmapMarkerDrag((int)index, markertag,(double)jlat, (double)jlng);
	}

	JNIEXPORT void JNICALL Java_com_myamap_tool_MyAmapViewHelper_onAmapMarkerDragEnd(JNIEnv *env, jclass, jint index, jstring jmarkertag, jdouble jlat, jdouble jlng)
	{
		string markertag = JniHelper::jstring2string(jmarkertag);

		AmapViewImpl::onAmapMarkerDragEnd((int)index, markertag,(double)jlat, (double)jlng);
	}

	JNIEXPORT void JNICALL Java_com_myamap_tool_MyAmapViewHelper_onAmapMarkerClick(JNIEnv *env, jclass, jint index, jstring jmarkertag)
	{
		string markertag = JniHelper::jstring2string(jmarkertag);

		AmapViewImpl::onAmapMarkerClick((int)index, markertag);
	}

	JNIEXPORT void JNICALL Java_com_myamap_tool_MyAmapViewHelper_onAmapMarkerInfoWindowClick(JNIEnv *env, jclass, jint index, jstring jmarkertag)
	{
		string markertag = JniHelper::jstring2string(jmarkertag);

		AmapViewImpl::onAmapMarkerInfoWindowClick((int)index, markertag);
	}

	JNIEXPORT void JNICALL Java_com_myamap_tool_MyAmapViewHelper_onDriveRouteSearched(JNIEnv *env, jclass, jint index, jint jcode, jdouble jslat, jdouble jslng, jdouble jelat, jdouble jelng, jint jsumtime, jint jsumdistance, jfloat jtaxicost, jstring jdiscription, jfloatArray jdistanceArray, jfloatArray jdurationArray, jobjectArray jinstructArray, jobjectArray jorientationArray)
	{
		DriveRouteData data;

		if ((int)jcode == 0)
		{

			data.slat = (double)jslat;
			data.slng = (double)jslng;

			data.elat = (double)jelat;
			data.elng = (double)jelng;

			data.distance = (int)jsumdistance;
			data.duration = (int)jsumtime;
			data.taxiCost = (float)jtaxicost;
			data.desctription = JniHelper::jstring2string(jdiscription);

			int psize = (int)env->GetArrayLength(jdistanceArray);

			data.v_steps.reserve(psize);

			jfloat* jdistance = env->GetFloatArrayElements(jdistanceArray, NULL);
			jfloat* jduration = env->GetFloatArrayElements(jdurationArray, NULL);
			for (int i = 0; i < psize; ++i)
			{
				RouteStep step;

				step.distance = (float)jdistance[i];
				step.duration = (float)jduration[i];

				jstring jinstructstr = (jstring)env->GetObjectArrayElement(jinstructArray, i);
				string instruct = JniHelper::jstring2string(jinstructstr);
				env->DeleteLocalRef(jinstructstr);
				step.instruct = instruct;

				jstring jorientationstr = (jstring)env->GetObjectArrayElement(jorientationArray, i);
				string orientation = JniHelper::jstring2string(jorientationstr);
				env->DeleteLocalRef(jorientationstr);
				step.orientation = orientation;

				data.v_steps.push_back(step);
			}

			env->ReleaseFloatArrayElements(jdistanceArray, jdistance, 0);
			env->ReleaseFloatArrayElements(jdurationArray, jduration, 0);
		}

		AmapViewImpl::onDriveRouteSearched((int)index, (int)jcode, data);
	}

	JNIEXPORT void JNICALL Java_com_myamap_tool_MyAmapViewHelper_onWalkRouteSearched(JNIEnv *env, jclass, jint index, jint jcode, jdouble jslat, jdouble jslng, jdouble jelat, jdouble jelng, jint jsumtime, jint jsumdistance, jstring jdiscription, jfloatArray jdistanceArray, jfloatArray jdurationArray, jobjectArray jinstructArray, jobjectArray jorientationArray)
	{
		WalkRouteData data;

		if ((int)jcode == 0)
		{
			data.slat = (double)jslat;
			data.slng = (double)jslng;

			data.elat = (double)jelat;
			data.elng = (double)jelng;

			data.distance = (int)jsumdistance;
			data.duration = (int)jsumtime;
			data.description = JniHelper::jstring2string(jdiscription);

			int psize = (int)env->GetArrayLength(jdistanceArray);

			data.v_steps.reserve(psize);

			jfloat* jdistance = env->GetFloatArrayElements(jdistanceArray, NULL);
			jfloat* jduration = env->GetFloatArrayElements(jdurationArray, NULL);
			for (int i = 0; i < psize; ++i)
			{
				RouteStep step;

				step.distance = (float)jdistance[i];
				step.duration = (float)jduration[i];

				jstring jinstructstr = (jstring)env->GetObjectArrayElement(jinstructArray, i);
				string instruct = JniHelper::jstring2string(jinstructstr);
				env->DeleteLocalRef(jinstructstr);
				step.instruct = instruct;

				jstring jorientationstr = (jstring)env->GetObjectArrayElement(jorientationArray, i);
				string orientation = JniHelper::jstring2string(jorientationstr);
				env->DeleteLocalRef(jorientationstr);
				step.orientation = orientation;

				data.v_steps.push_back(step);
			}

			env->ReleaseFloatArrayElements(jdistanceArray, jdistance, 0);
			env->ReleaseFloatArrayElements(jdurationArray, jduration, 0);
		}

		AmapViewImpl::onWalkRouteSearched((int)index, (int)jcode, data);
	}

	JNIEXPORT void JNICALL Java_com_myamap_tool_MyAmapViewHelper_onBusRouteSearched(JNIEnv *env, jclass, jint index, jint jcode, jdoubleArray jslatArray, jdoubleArray jslngArray, jdoubleArray jelatArray, jdoubleArray jelngArray, jintArray jtagArray, jintArray jdistanceArray, jintArray jdurationArray, jfloatArray jcostArray, jobjectArray jpathTitleArray, jobjectArray jpathDesArray, jfloatArray jbusdisArray, jfloatArray jwalkdisArray)
	{
		std::vector<BusRouteData> v_datas;

		if ((int)jcode == 0)
		{
			int psize = (int)env->GetArrayLength(jtagArray);

			v_datas.reserve(psize);

			jdouble* jslat = env->GetDoubleArrayElements(jslatArray, NULL);
			jdouble* jslng = env->GetDoubleArrayElements(jslngArray, NULL);
			jdouble* jelat = env->GetDoubleArrayElements(jelatArray, NULL);
			jdouble* jelng = env->GetDoubleArrayElements(jelngArray, NULL);

			jint* jtag = env->GetIntArrayElements(jtagArray, NULL);
			jint* jdistance = env->GetIntArrayElements(jdistanceArray, NULL);
			jint* jduration = env->GetIntArrayElements(jdurationArray, NULL);

			jfloat* jcost = env->GetFloatArrayElements(jcostArray, NULL);
			jfloat* jbusdis = env->GetFloatArrayElements(jbusdisArray, NULL);
			jfloat* jwalkdis = env->GetFloatArrayElements(jwalkdisArray, NULL);

			for (int i = 0; i < psize; ++i)
			{
				BusRouteData path;

				path.slng = (double)jslng[i];
				path.slat = (double)jslat[i];

				path.elat = (double)jelat[i];
				path.elng = (double)jelng[i];

				path.tag = (int)jtag[i];
				path.distance = (int)jdistance[i];
				path.duration = (int)jduration[i];

				path.cost = (float)jcost[i];
				path.busDistance = (float)jbusdis[i];
				path.walkDistance = (float)jwalkdis[i];


				jstring jpathTitleStr = (jstring)env->GetObjectArrayElement(jpathTitleArray, i);
				string pathTitle = JniHelper::jstring2string(jpathTitleStr);
				env->DeleteLocalRef(jpathTitleStr);
				path.pathTitle = pathTitle;

				jstring jpathDesStr = (jstring)env->GetObjectArrayElement(jpathDesArray, i);
				string pathDes = JniHelper::jstring2string(jpathDesStr);
				env->DeleteLocalRef(jpathDesStr);
				path.pathDes = pathDes;

				v_datas.push_back(path);
			}

			env->ReleaseDoubleArrayElements(jslatArray, jslat, 0);
			env->ReleaseDoubleArrayElements(jslngArray, jslng, 0);
			env->ReleaseDoubleArrayElements(jelatArray, jelat, 0);
			env->ReleaseDoubleArrayElements(jelngArray, jelng, 0);

			env->ReleaseIntArrayElements(jtagArray, jtag, 0);
			env->ReleaseIntArrayElements(jdistanceArray, jdistance, 0);
			env->ReleaseIntArrayElements(jdurationArray, jduration, 0);

			env->ReleaseFloatArrayElements(jcostArray, jcost, 0);
			env->ReleaseFloatArrayElements(jbusdisArray, jbusdis, 0);
			env->ReleaseFloatArrayElements(jwalkdisArray, jwalkdis, 0);
		}

		AmapViewImpl::onBusRouteSearched((int)index, (int)jcode, v_datas);
	}

	JNIEXPORT void JNICALL Java_com_myamap_tool_MyAmapViewHelper_onBusRouteSteps(JNIEnv *env, jclass, jint index, jint jcode, jint jbustag, jintArray jtypeArray, jobjectArray jnameArray, jfloatArray jdistanceArray, jfloatArray jdurationArray, jobjectArray jstationsArray)
	{
		std::vector<BusRouteStep> v_datas;

		if ((int)jcode == 0)
		{
			int psize = (int)env->GetArrayLength(jtypeArray);

			v_datas.reserve(psize);

			jint* jtype = env->GetIntArrayElements(jtypeArray, NULL);

			jfloat* jdistance = env->GetFloatArrayElements(jdistanceArray, NULL);
			jfloat* jduration = env->GetFloatArrayElements(jdurationArray, NULL);

			for (int i = 0; i < psize; ++i)
			{
				BusRouteStep step;

				step.type = (int)jtype[i];

				step.distance = (float)jdistance[i];
				step.duration = (float)jduration[i];


				jstring jnameStr = (jstring)env->GetObjectArrayElement(jnameArray, i);
				string name = JniHelper::jstring2string(jnameStr);
				env->DeleteLocalRef(jnameStr);
				step.name = name;

				jstring jstationsStr = (jstring)env->GetObjectArrayElement(jstationsArray, i);
				string stations = JniHelper::jstring2string(jstationsStr);
				env->DeleteLocalRef(jstationsStr);
				step.stations = stations;

				v_datas.push_back(step);
			}

			env->ReleaseIntArrayElements(jtypeArray, jtype, 0);

			env->ReleaseFloatArrayElements(jdistanceArray, jdistance, 0);
			env->ReleaseFloatArrayElements(jdurationArray, jduration, 0);
		}

		AmapViewImpl::onBusRouteSteps((int)index, (int)jcode, (int)jbustag, v_datas);
	}
}

#else

int CreateAMap(float x, float y, float width, float height)
{
	return 0;
}
void DestroyAMap(const int tag)
{}
void SetMapCenter(const int tag, double lat, double lng)
{}
void UpdateFrame(const int tag, int x, int y, int width, int height)
{}
void SetVisible(const int tag, bool visible)
{}
void SetMapZoomLevel(const int tag, double level)
{}
void AddMarker(const int tag, const string& markertag, AmapMarker* marker)
{}
void DeleteMarker(const int tag, const string& markertag)
{}
void ClearMarkers(const int tag)
{}
void StartAmapLocation(const int tag)
{}
void SetAmapRouteViewType(const int tag, double slat, double slng, double elat, double elng, int type)
{}
void SearchRoute(const int tag)
{}
void ShowBusRouteView(const int _viewTag, const int busTag)
{}
void SetBusRouteCity(const int _viewTag, const string& city, const string& cityd)
{}
void GetBusRouteStep(const int _viewTag, const int bustag)
{}

#endif

static std::map<int, AmapViewImpl*> s_AmapViewImpls;
AmapViewImpl::AmapViewImpl(AmapView *mapView) : m_pAmapView(mapView)
{
	DRect cRect = m_pAmapView->convertRectToWorldSpace(m_pAmapView->getBounds());
	cRect.origin.x = s_dip_to_px(cRect.origin.x);
	cRect.origin.y = s_dip_to_px(cRect.origin.y);
	cRect.size.width = s_dip_to_px(cRect.size.width);
	cRect.size.height = s_dip_to_px(cRect.size.height);

	_viewTag = CreateAMap(cRect.origin.x, cRect.origin.y, cRect.size.width, cRect.size.height);

	s_AmapViewImpls[_viewTag] = this;
}

AmapViewImpl::~AmapViewImpl()
{
	clearMarkers();

	DestroyAMap(_viewTag);
	s_AmapViewImpls.erase(_viewTag);
}

void AmapViewImpl::setMapCenter(double lat, double lng)
{
	SetMapCenter(_viewTag, lat, lng);
}

void AmapViewImpl::startLocation()
{
	StartAmapLocation(_viewTag);
}

void AmapViewImpl::setMapZoomLevel(double level)
{
	SetMapZoomLevel(_viewTag, level);
}

void AmapViewImpl::update(float dt)
{
	DRect cRect = m_pAmapView->convertRectToWorldSpace(m_pAmapView->getBounds());
	cRect.origin.x = s_dip_to_px(cRect.origin.x);
	cRect.origin.y = s_dip_to_px(cRect.origin.y);
	cRect.size.width = s_dip_to_px(cRect.size.width);
	cRect.size.height = s_dip_to_px(cRect.size.height);

	UpdateFrame(_viewTag, cRect.origin.x, cRect.origin.y, cRect.size.width, cRect.size.height);
}

void AmapViewImpl::setVisible(bool visible)
{
	SetVisible(_viewTag, visible);
}

void AmapViewImpl::addMarker(AmapMarker* marker)
{
	marker->retain();

	s_AmapMarkers[crossapp_format_string("%p", marker)] = marker;
	AddMarker(_viewTag, crossapp_format_string("%p", marker), marker);
}

void AmapViewImpl::deleteMarker(AmapMarker* marker)
{
	string markertag = crossapp_format_string("%p", marker);
	std::map<string, AmapMarker*>::iterator it = s_AmapMarkers.find(markertag);
	if (it != s_AmapMarkers.end())
	{
		s_AmapMarkers.erase(it);
		CC_SAFE_RELEASE_NULL(marker);
		DeleteMarker(_viewTag, markertag);
	}
}

void AmapViewImpl::clearMarkers()
{
	for (std::map<string, AmapMarker*>::iterator it = s_AmapMarkers.begin(); it != s_AmapMarkers.end(); ++it)
	{
		CC_SAFE_RELEASE_NULL(it->second);
	}

	s_AmapMarkers.clear();

	ClearMarkers(_viewTag);
}

void AmapViewImpl::setAmapRouteViewType(double slat, double slng, double elat, double elng, AmapRouteViewType type)
{
	SetAmapRouteViewType(_viewTag, slat, slng, elat, elng, (int)type);
}

void AmapViewImpl::searchRoute()
{
	SearchRoute(_viewTag);
}

void AmapViewImpl::showBusRouteView(const int busTag)
{
	ShowBusRouteView(_viewTag, busTag);
}

void AmapViewImpl::setBusRouteCity(const string& city, const string& cityd)
{
	SetBusRouteCity(_viewTag, city, cityd);
}

void AmapViewImpl::getBusRouteStep(int bustag)
{
	GetBusRouteStep(_viewTag, bustag);
}

std::map<string, AmapMarker*>& AmapViewImpl::getAllMarkers()
{
	return s_AmapMarkers;
}

void AmapViewImpl::onAmapLoaded(const int viewTag)
{
	std::map<int, AmapViewImpl*>::iterator it = s_AmapViewImpls.find(viewTag);
	if (it != s_AmapViewImpls.end())
	{
		AmapView* ampView = it->second->m_pAmapView;
		if (ampView && ampView->getAmapViewDelegate())
		{
			ampView->getAmapViewDelegate()->onAmapLoaded();
		}
	}
}

void AmapViewImpl::onAmapClicked(const int viewTag, double lat, double lng)
{
	std::map<int, AmapViewImpl*>::iterator it = s_AmapViewImpls.find(viewTag);
	if (it != s_AmapViewImpls.end())
	{
		AmapView* ampView = it->second->m_pAmapView;
		if (ampView && ampView->getAmapViewDelegate())
		{
			ampView->getAmapViewDelegate()->onAmapClicked(lat, lng);
		}
	}
}

void AmapViewImpl::onAmapLocationEnd(const int viewTag, int code, double lat, double lng, const string& formatAddr, const string& province, const string& city, const string& district, const string& street, const string& number, const string& poi, const string& adcode)
{
	std::map<int, AmapViewImpl*>::iterator it = s_AmapViewImpls.find(viewTag);
	if (it != s_AmapViewImpls.end())
	{
		AmapView* ampView = it->second->m_pAmapView;
		if (ampView && ampView->getAmapViewDelegate())
		{
			ampView->getAmapViewDelegate()->onAmapLocationEnd(code, lat, lng, formatAddr, province, city, district, street, number, poi, adcode);
		}
	}
}

void AmapViewImpl::onAmapMarkerDragStart(const int viewTag, const string& markertag, double lat, double lng)
{
	std::map<int, AmapViewImpl*>::iterator it = s_AmapViewImpls.find(viewTag);
	if (it != s_AmapViewImpls.end())
	{
		std::map<string, AmapMarker*>::iterator mIt = it->second->getAllMarkers().find(markertag);
		if (mIt != it->second->getAllMarkers().end())
		{
			AmapView* ampView = it->second->m_pAmapView;
			if (ampView && ampView->getAmapViewDelegate())
			{
				ampView->getAmapViewDelegate()->onAmapMarkerDragStart(mIt->second, lat, lng);
			}
		}
	}
}

void AmapViewImpl::onAmapMarkerDrag(const int viewTag, const string& markertag, double lat, double lng)
{
	std::map<int, AmapViewImpl*>::iterator it = s_AmapViewImpls.find(viewTag);
	if (it != s_AmapViewImpls.end())
	{
		std::map<string, AmapMarker*>::iterator mIt = it->second->getAllMarkers().find(markertag);
		if (mIt != it->second->getAllMarkers().end())
		{
			AmapView* ampView = it->second->m_pAmapView;
			if (ampView && ampView->getAmapViewDelegate())
			{
				ampView->getAmapViewDelegate()->onAmapMarkerDrag(mIt->second, lat, lng);
			}
		}
	}
}

void AmapViewImpl::onAmapMarkerDragEnd(const int viewTag, const string& markertag, double lat, double lng)
{
	std::map<int, AmapViewImpl*>::iterator it = s_AmapViewImpls.find(viewTag);
	if (it != s_AmapViewImpls.end())
	{
		std::map<string, AmapMarker*>::iterator mIt = it->second->getAllMarkers().find(markertag);
		if (mIt != it->second->getAllMarkers().end())
		{
			AmapView* ampView = it->second->m_pAmapView;
			if (ampView && ampView->getAmapViewDelegate())
			{
				ampView->getAmapViewDelegate()->onAmapMarkerDragEnd(mIt->second, lat, lng);
			}
		}
	}
}

void AmapViewImpl::onAmapMarkerClick(const int viewTag, const string& markertag)
{
	std::map<int, AmapViewImpl*>::iterator it = s_AmapViewImpls.find(viewTag);
	if (it != s_AmapViewImpls.end())
	{
		std::map<string, AmapMarker*>::iterator mIt = it->second->getAllMarkers().find(markertag);
		if (mIt != it->second->getAllMarkers().end())
		{
			AmapView* ampView = it->second->m_pAmapView;
			if (ampView && ampView->getAmapViewDelegate())
			{
				ampView->getAmapViewDelegate()->onAmapMarkerClick(mIt->second);
			}
		}
	}
}

void AmapViewImpl::onAmapMarkerInfoWindowClick(const int viewTag, const string& markertag)
{
	std::map<int, AmapViewImpl*>::iterator it = s_AmapViewImpls.find(viewTag);
	if (it != s_AmapViewImpls.end())
	{
		std::map<string, AmapMarker*>::iterator mIt = it->second->getAllMarkers().find(markertag);
		if (mIt != it->second->getAllMarkers().end())
		{
			AmapView* ampView = it->second->m_pAmapView;
			if (ampView && ampView->getAmapViewDelegate())
			{
				ampView->getAmapViewDelegate()->onAmapMarkerInfoWindowClick(mIt->second);
			}
		}
	}
}

void AmapViewImpl::onDriveRouteSearched(const int viewTag, int code, DriveRouteData data)
{
	std::map<int, AmapViewImpl*>::iterator it = s_AmapViewImpls.find(viewTag);
	if (it != s_AmapViewImpls.end())
	{
		AmapView* ampView = it->second->m_pAmapView;
		if (ampView && ampView->getAmapViewDelegate())
		{
			ampView->getAmapViewDelegate()->onDriveRouteSearched(code, data);
		}
	}
}

void AmapViewImpl::onWalkRouteSearched(const int viewTag, int code, WalkRouteData data)
{
	std::map<int, AmapViewImpl*>::iterator it = s_AmapViewImpls.find(viewTag);
	if (it != s_AmapViewImpls.end())
	{
		AmapView* ampView = it->second->m_pAmapView;
		if (ampView && ampView->getAmapViewDelegate())
		{
			ampView->getAmapViewDelegate()->onWalkRouteSearched(code, data);
		}
	}
}

void AmapViewImpl::onBusRouteSearched(const int viewTag, int code, std::vector<BusRouteData> data)
{
	std::map<int, AmapViewImpl*>::iterator it = s_AmapViewImpls.find(viewTag);
	if (it != s_AmapViewImpls.end())
	{
		AmapView* ampView = it->second->m_pAmapView;
		if (ampView && ampView->getAmapViewDelegate())
		{
			ampView->getAmapViewDelegate()->onBusRouteSearched(code, data);
		}
	}
}

void AmapViewImpl::onBusRouteSteps(const int viewTag, int code, const int bustag, std::vector<BusRouteStep> data)
{
	std::map<int, AmapViewImpl*>::iterator it = s_AmapViewImpls.find(viewTag);
	if (it != s_AmapViewImpls.end())
	{
		AmapView* ampView = it->second->m_pAmapView;
		if (ampView && ampView->getAmapViewDelegate())
		{
			ampView->getAmapViewDelegate()->onBusRouteSteps(code, bustag, data);
		}
	}
}
